var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require('lodash');

router.get('/', function(req, res, next) {
  
    let equipmentid = _.trim(String(req.query.equipmentid));

    let data = {
        isSuccess: false,
        data: null,
        msg: '关闭设备送修状态失败！'
    };
    
   db.changeState(equipmentid, '1', function(err, rows) {
        if(err) {
            console.log("出错啦" + err)
            return res.json(data);
        } else {
            if(rows.affectedRows > 0 ) {
                let changeMaintainStateSql = "UPDATE equipment_maintenance SET isfinished=1, finisheddate='" + new Date().toLocaleDateString() +"' WHERE equipmentid='" + equipmentid + "' and isfinished=0"
                db.execute(changeMaintainStateSql, function(error, row) {
                    if(error) {
                        return res.json(data);
                    } else {
                        if(row.affectedRows > 0 ) {
                            console.log("成功")
                            data.isSuccess = true;
                            data.msg = "关闭设备送修状态成功！";
                            return res.json(data);
                        } else {
                            return res.json(data);
                        }
                    }
                })
            }
        }
   })
});

module.exports = router;